import 'dart:math';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:study_flutter/yzhg/module/MConstant.dart';

import 'dio/get_post_dio.dart';

/*
 * 三方网络请求库：
 *    地址：https://github.com/flutterchina/dio
 *    https://www.jianshu.com/p/fc7debbb22ae
 */
class DioHttp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dio网络实战"),
      ),
      body: DioHttpState(),
    );
  }
}

class DioHttpState extends StatefulWidget {
  @override
  _DioHttpState createState() => _DioHttpState();
}

class _DioHttpState extends State<DioHttpState> {
  //创建颜色集合
  var colorList = [
    Colors.greenAccent,
    Colors.green,
    Colors.blue,
    Colors.red,
    Colors.amber,
    Colors.deepPurple,
    Colors.pink,
    Colors.blueGrey,
    Colors.indigo,
    Colors.brown,
  ];

  var random = new Random();

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      crossAxisCount: 2,
      mainAxisSpacing: 12,
      crossAxisSpacing: 12,
      childAspectRatio: 1,
      children: <Widget>[
        _gradViewItem("get/post请求", MConstant.DIO_GET_HTTP),
        _gradViewItem("封装网络请求", MConstant.DIO_ENCAPSULATION_HTTP),
      ],
    );
  }

  _gradViewItem(String content, String navigator) {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.all(8),
      decoration: new BoxDecoration(
          color: colorList[random.nextInt(colorList.length)],
          borderRadius: new BorderRadius.all(const Radius.circular(4))),
      child: MaterialButton(
        //充满父控件
        minWidth: double.infinity,
        height: double.infinity,
        onPressed: () {
          Navigator.pushNamed(context, navigator);
        },
        child: Text(
          content,
          style: TextStyle(fontSize: 14, color: Colors.white),
        ),
      ),
    );
  }
}
